// 文件路径: com/rainkaze/lifeharbor/tools/weather/adapter/HourlyForecastAdapter.java
// (如果adapter包不存在，请创建它 com.rainkaze.lifeharbor.tools.weather.adapter)
package com.rainkaze.lifeharbor.tools.weather.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rainkaze.lifeharbor.R;
import com.rainkaze.lifeharbor.tools.weather.model.ForecastHourly; // 确保导入
import com.rainkaze.lifeharbor.tools.weather.model.HourlyTemperature;
import com.rainkaze.lifeharbor.tools.weather.model.HourlyWeather;
import com.rainkaze.lifeharbor.tools.weather.model.HourlyWindItem;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class HourlyForecastAdapter extends RecyclerView.Adapter<HourlyForecastAdapter.ViewHolder> {

    private static final String TAG = "HourlyForecastAdapter";
    private List<HourlyForecastItem> hourlyItems; // 将使用一个包装类
    private Context context;
    private Map<String, String> weatherCodeToDescriptionMap;
    private Map<String, Integer> weatherCodeToIconMap;

    // 内部类，用于整合每小时的数据
    public static class HourlyForecastItem {
        public String time;
        public String temperature;
        public String weatherCode;

        public HourlyForecastItem(String time, String temperature, String weatherCode) {
            this.time = time;
            this.temperature = temperature;
            this.weatherCode = weatherCode;
        }
    }

    public HourlyForecastAdapter(Context context,
                                 List<HourlyForecastItem> hourlyItems,
                                 Map<String, String> weatherCodeToDescriptionMap,
                                 Map<String, Integer> weatherCodeToIconMap) {
        this.context = context;
        this.hourlyItems = hourlyItems != null ? hourlyItems : new ArrayList<>();
        this.weatherCodeToDescriptionMap = weatherCodeToDescriptionMap;
        this.weatherCodeToIconMap = weatherCodeToIconMap;
    }

    public void updateData(List<HourlyForecastItem> newHourlyItems) {
        this.hourlyItems.clear();
        if (newHourlyItems != null) {
            this.hourlyItems.addAll(newHourlyItems);
        }
        notifyDataSetChanged();
        Log.d(TAG, "Hourly data updated. Item count: " + this.hourlyItems.size());
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_hourly_weather, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HourlyForecastItem item = hourlyItems.get(position);

        holder.textViewHourlyTime.setText(item.time);
        holder.textViewHourlyTemperature.setText(item.temperature);

        String weatherCodeStr = item.weatherCode;
        holder.imageViewHourlyWeatherIcon.setImageResource(getWeatherIconResId(weatherCodeStr));
        // 可选：设置 contentDescription 用于无障碍
        // holder.imageViewHourlyWeatherIcon.setContentDescription(getWeatherDescription(weatherCodeStr));
    }

    @Override
    public int getItemCount() {
        return hourlyItems.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textViewHourlyTime;
        ImageView imageViewHourlyWeatherIcon;
        TextView textViewHourlyTemperature;

        ViewHolder(View itemView) {
            super(itemView);
            textViewHourlyTime = itemView.findViewById(R.id.textViewHourlyTime);
            imageViewHourlyWeatherIcon = itemView.findViewById(R.id.imageViewHourlyWeatherIcon);
            textViewHourlyTemperature = itemView.findViewById(R.id.textViewHourlyTemperature);
        }
    }

    private String getWeatherDescription(String code) {
        if (code == null || weatherCodeToDescriptionMap == null) return "未知";
        return weatherCodeToDescriptionMap.getOrDefault(code, "代码 " + code);
    }

    private int getWeatherIconResId(String code) {
        if (code == null || weatherCodeToIconMap == null) return R.drawable.unknwn_weather;
        return weatherCodeToIconMap.getOrDefault(code, R.drawable.unknwn_weather);
    }
}
